#include "nr_client_dataAcquisition.h"
#include "ui_nr_client_dataAcquisition.h"
#include "nr_data.h"


subwindow1::subwindow1(QWidget *parent, Qt::WFlags flags)
	: QWidget(parent, flags)
{
	ui = new Ui::subwindow1;
	ui->setupUi(this);
	
	QPalette palette;
	palette.setBrush(QPalette::Background, QBrush(QPixmap("systemhomepage.png")));
	this->setPalette(palette);
	ui->tvhissearch->setSelectionBehavior(QAbstractItemView::SelectRows);  
	
	connect(ui->pbhisquery,SIGNAL(pressed()),this,SLOT(on_pbhisquery_clicked()));
	//connect(ui.treeView,SIGNAL(clicked(QModelIndex)),this,SLOT(on_treeView_Clicked()));
	//	iterateOverItems();

}

subwindow1::~subwindow1()
{
	delete ui;
	
}

// void subwindow1::set_client_dataAcquisition( QWidget *_last_mdiArea_Widget )
// {
// 	m_last_mdiArea_Widget = _last_mdiArea_Widget;
// }

void subwindow1::set_device_TreeWidgetItem( DeviceTreeWidgetItem &_device_TreeWidgetItem )
{
	m_device_TreeWidgetItem=&_device_TreeWidgetItem;
}

void subwindow1::set_device_type_manager( nr_device_type_manager &_device_type_manager )
{
	m_device_type_manager=&_device_type_manager;
	QStringList str_list;
	foreach( nr_device_type* devicetype, m_device_type_manager->device_types() )
	{
		str_list << devicetype->text();
	}
	//ui->comboBox->addItems( str_list );
}

void subwindow1::set_DataCommunication(client_Communication& clientdeviceTree)//translate data to subwindow1;
{
	m_clientdeviceTree = &clientdeviceTree;

	nr_device_item* deviceitem = (nr_device_item*)m_device_TreeWidgetItem->object_device();

	QList<nr_device_id_t> device_id_list;
	device_id_list.push_back( deviceitem->id() );
	m_clientdeviceTree->start_GetdataFromServer(device_id_list);

	connect(m_clientdeviceTree,SIGNAL(client_received_data(nr_data* )),this,SLOT(on_showdata(nr_data* )));
}

void subwindow1::on_showdata(nr_data* data)
{
	ui->lb_deviceID_2->setText( (QString)data->device_id() );
	
	for(QVariantHash::iterator it = data->args().begin() ;it!=data->args().end(); ++it)
	{
		if (it.key() == "Uan")
		{
			ui->lcduan->display(it.value().toDouble());
		}
		else if (it.key() == "Ubn")
		{
			ui->lcdubn->display(it.value().toDouble());
		}
		else if (it.key() == "Ucn")
		{
			ui->lcducn->display(it.value().toDouble());
		}
		else if (it.key() == "Uab")
		{
			ui->lcduab->display(it.value().toDouble());
		}
		else if (it.key() == "Ubc")
		{
			ui->lcdubc->display(it.value().toDouble());
		}
		else if (it.key() == "Uac")
		{
			ui->lcduca->display(it.value().toDouble());
		}
		else if (it.key() == "Ia")
		{
			ui->lcdia->display(it.value().toDouble());
		}
		else if (it.key() == "Ib")
		{
			ui->lcdib->display(it.value().toDouble());
		}
		else if (it.key() == "Ic")
		{
			ui->lcdic->display(it.value().toDouble());
		}
		else if (it.key() == "F")
		{
			ui->lcdhz->display(it.value().toDouble());
		}
	}
}

void subwindow1::set_querydata_Communication()
{
	nr_device_item* deviceitem = (nr_device_item*)m_device_TreeWidgetItem->object_device();

	QDateTime beginTime = ui->startdatetime->dateTime();
	QDateTime endTime = ui->enddatetime->dateTime();
	QString str_beginTime = beginTime.toString();
	QString str_endTime = endTime.toString();

	m_clientdeviceTree->start_QuerydataFromServer(deviceitem->id(),
		ui->startdatetime->dateTime(),ui->enddatetime->dateTime() );

	bool ret = connect(m_clientdeviceTree,SIGNAL(client_received_hisdata( QList<nr_data*>& )),
		this,SLOT(on_showhisdata( QList<nr_data*>& )));
	 
}

void subwindow1::on_pbhisquery_clicked()
{
	this->set_querydata_Communication();
}

void subwindow1::on_showhisdata( QList<nr_data*>& data_list)
{
	QList<nr_data*>::iterator listiter=data_list.begin();
	nr_data *datatemp = NULL;
	QStandardItemModel* model = NULL;
	int column =0,row=0;
	model = new QStandardItemModel();
	for(listiter =data_list.begin();listiter != data_list.end();listiter++ )
	{		
		datatemp = *listiter;
		if(row==0)
		{		 					
			model->setHorizontalHeaderItem(column++, new QStandardItem( QObject::tr("Date/Time") ));//QObject::tr("Name"))
			for(QVariantHash::iterator it = datatemp->args().begin();it != datatemp->args().end(); ++it)
			{							    
				model->setHorizontalHeaderItem(column++, new QStandardItem( it.key() ));
			}		
		}//if row== 0,set tableview headeritem;
		//else
		{
			model->setItem(row,0,new QStandardItem( datatemp->time().toString() ));//insert datetime into each row,column 1;
			column =1;
	 		for(QVariantHash::iterator it_value = datatemp->args().begin();
				it_value != datatemp->args().end(); ++it_value)
			{
	 			model->setItem(row,column++,new QStandardItem(it_value.value().toString()));
			}
	 	}//insert data into tableview;
	 		row++;
	}
	ui->tvhissearch->setModel(model);	
}

 //void subwindow1::set_client_dataAcquisition(mainwindow &_mainwindow)
 //{
 //	m_mainwindow = &_mainwindow;
 //}